load('classpath:ext/db.js');

var env = "dev.test";

mysql_update(env, "CREATE TABLE IF NOT EXISTS `user` (\n" +
    "  `id` int(11) NOT NULL,\n" +
    "  `name` varchar(255) DEFAULT NULL,\n" +
    "  `create_time` datetime DEFAULT NULL,\n" +
    "  PRIMARY KEY (`id`)\n" +
    ") ENGINE=InnoDB");

sql = "select id from test.user where id = ?";
result = mysql_query(env, sql, 1);
if (result.size() === 0) {
    var update  = mysql_update(env, "INSERT INTO test.`user` (`id`, `name`, `create_time`) " +
        "VALUES ('1', 'aa', '2019-08-06 23:33:14');");
    console.info("add user id = 1, status = " + update)
}

var sql = "delete from test.user where id = ?";
update = mysql_update(env, sql, 2);
console.info("delete user id = 2, status = " + update);

sql = "insert into test.user(id,name,create_time) values (2, 'bb', now());";
update = mysql_update(env, sql);
console.info("insert user id = 2, status = " + update);

sql = "select id,name,create_time from test.user";
result = mysql_query(env, sql);
console.info(JSON.toJson(result));

sql = "select id,name,create_time from test.user where id = ?";
result = mysql_query(env, sql, 2);
console.info(JSON.toJson(result));